package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import org.json.JSONObject;

import java.util.HashMap;


public class ChangePassActivity extends BaseActivity implements View.OnClickListener {

    private InputMethodManager manager;
    private RelativeLayout re_back;
    private TextView tv_save;
    private EditText ed_old_pass,ed_new_pass,ed_confirm_pass;
    private ImageView iv_yeye,xpass_eye,img_confirm;
    private int old_pass = 0,new_pass = 0,confirm_pass = 0;

    @Override
    public int intiLayout() {
        return R.layout.activity_change_pass;
    }

    @Override
    public void initView() {

        manager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        re_back=findViewById(R.id.re_back);
        re_back.setOnClickListener(this);
        tv_save = findViewById(R.id.tv_save);
        tv_save.setOnClickListener(this);
        ed_old_pass = findViewById(R.id.ed_old_pass);
        ed_new_pass = findViewById(R.id.ed_new_pass);
        ed_confirm_pass = findViewById(R.id.ed_confirm_pass);
        iv_yeye = findViewById(R.id.iv_yeye);
        iv_yeye.setOnClickListener(this);
        xpass_eye = findViewById(R.id.xpass_eye);
        xpass_eye.setOnClickListener(this);
        img_confirm = findViewById(R.id.img_confirm);
        img_confirm.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {

        if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
            manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }

        switch (v.getId()){

            //返回键
            case R.id.re_back:

                finish();
                break;

            //查看原密码
            case R.id.iv_yeye:

                if (old_pass == 0){

                    old_pass = 1;
                    iv_yeye.setImageResource(R.drawable.eye_open);
                    ed_old_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {

                    old_pass = 0;
                    iv_yeye.setImageResource(R.drawable.eye_close);
                    ed_old_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }

                ed_old_pass.setSelection(ed_old_pass.getText().toString().length());
                break;

            //查看新密码
            case R.id.xpass_eye:

                if (new_pass == 0){

                    new_pass = 1;
                    xpass_eye.setImageResource(R.drawable.eye_open);
                    ed_new_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {

                    new_pass = 0;
                    xpass_eye.setImageResource(R.drawable.eye_close);
                    ed_new_pass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    ed_new_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }

                ed_new_pass.setSelection(ed_new_pass.getText().toString().length());
                break;

            //查看确认密码
            case R.id.img_confirm:

                if (confirm_pass == 0){

                    confirm_pass = 1;
                    img_confirm.setImageResource(R.drawable.eye_open);
                    ed_confirm_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {

                    confirm_pass = 0;
                    img_confirm.setImageResource(R.drawable.eye_close);
                    ed_confirm_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }

                ed_confirm_pass.setSelection(ed_confirm_pass.getText().toString().length());
                break;

            //保存
            case R.id.tv_save:

                String old_pass = ed_old_pass.getText().toString().trim();
                String new_pass = ed_new_pass.getText().toString().trim();
                String confirm_pass = ed_confirm_pass.getText().toString().trim();

                if (old_pass.length() == 0){

                    toastShort("请输入原密码");
                }else if (new_pass.length() == 0){

                    toastShort("请输入新密码");
                }else if (new_pass.length() < 6){

                    toastShort("请输入6~12位的新密码");
                }else if (confirm_pass.length() == 0){

                    toastShort("请再次输入新密码");
                }else if (!new_pass.equals(confirm_pass)){

                    toastShort("两次密码不一致");
                }else {

                    getData(new_pass,old_pass);
                }
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){

                case 1:

                    try{

                        JSONObject jsonObject = new JSONObject((String) msg.obj);

                        if (jsonObject.optString("code").equals("0")){

                            finish();
                        }

                        Toast.makeText(ChangePassActivity.this,jsonObject.optString("msg"),Toast.LENGTH_SHORT).show();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(ChangePassActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(ChangePassActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(ChangePassActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private void getData(String pass,String old){
        int mark= (int) SpUtils.get(this,"mark",0);
        HashMap<String,Object> map = new HashMap<>();
        map.put("token", SpUtils.get(this,"token",""));
        map.put("mark", SpUtils.get(this,"mark",1));
        map.put("password", pass);
        map.put("oldPassword", old);
        if(mark==1){
            map.put("id", SpUtils.get(this,"ysid",0));
        }else{
            map.put("id", SpUtils.get(this,"userid",0));
        }
        OkHttpUtils.getNetLoginWork(this, Constant.baseurl+"online/user/updatePassword",map,handler);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN){

            if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}
